<?php
  //-----------------------Beginning of the Main----------------------------
  header('Content-Type: text/javascript');
  header('Cache-Control: no-cache');
  header('Pragma: no-cache');
  
  $gGalleryPath = "../pic";
  $gDefaultLanguage = "english";
  //$gDefaultLanguage = "chinese";
  
  class GroupInfo
  {
    public $groupName="";
    public $groupTitle="";
    public $groupFiles="";    
    public $fileDescriptions="";
  }
  
  $direction = trim($_GET['direction']);
  $currentGroup = trim($_GET['currentgroup']);
    
  $groupInfo = new GroupInfo();  
  
  if($direction == 'next')
  {
    $nextGroup = getNextGroup($currentGroup);
    $items = getGroupItems($nextGroup);
    
    $groupInfo->groupName = $nextGroup;       
    $groupInfo->groupFiles = $items;
    getMetaInfo($groupInfo->groupName,$gDefaultLanguage,$groupInfo);
  }
  else if($direction == 'previous')
  {
    $previousGroup = getPreviousGroup($currentGroup);    
    $items = getGroupItems($previousGroup);
    
    $groupInfo->groupName = $previousGroup;    
    $groupInfo->groupFiles = $items;
    getMetaInfo($groupInfo->groupName,$gDefaultLanguage,$groupInfo);
  }
  else if($direction == 'current')
  {
    $curGroup = getCurrentGroup($currentGroup);    
    $items = getGroupItems($curGroup);
    
    $groupInfo->groupName = $curGroup;
    
    $groupInfo->groupFiles = $items;
    getMetaInfo($groupInfo->groupName,$gDefaultLanguage,$groupInfo);
  }
  else
  {
    return;
  }
  
  echo json_encode($groupInfo);
  
  //-----------------------End of the Main----------------------------
  



  //Functions Implementation
  function getGroupList()
  {
    global $gGalleryPath;
    
    $groupList = scandir($gGalleryPath,0);  
    
    $groupListLen = sizeof($groupList);
    $count = 0;

    $ret = "";
    for(;$count < $groupListLen; $count++)
    {
      if($groupList[$count] != '.' && $groupList[$count] != '..')
      {
        $ret[] = $groupList[$count];
      }
    }        
    
    return $ret;
  }
  
  function getNextGroup($currentGroup)
  {    
    $groupList = getGroupList();
    
    if($currentGroup == NULL)
    {
      return $groupList[0];
    }
        
    $groupListLen = sizeof($groupList);
    $count = 0;
    for(;$count < $groupListLen; $count++)
    {
      if($groupList[$count] == $currentGroup)
      {                
        if($count + 1 < $groupListLen)
        {
          return $groupList[$count + 1];
        }        
        else
        {
          return $groupList[0];
        }
      }           
    }
  }
  
  function getPreviousGroup($currentGroup)
  {
    $groupList = getGroupList();
    
    if($currentGroup == NULL)
    {
      return $groupList[0];
    }
    
    $groupListLen = sizeof($groupList);
    $count = 0;          
    for(;$count < $groupListLen; $count++)
    {
      if($groupList[$count] == $currentGroup)
      {                
        if($count >= 1)
        {
          return $groupList[$count - 1];
        }        
        else
        {
          return $groupList[$groupListLen - 1];
        }
      }           
    }
  }
  
  function getCurrentGroup($currentGroup)
  {
    $groupList = getGroupList();
    
    if($currentGroup == NULL)
    {
      return $groupList[0];
    }
    
    $groupListLen = sizeof($groupList);
    $count = 0;          
    for(;$count < $groupListLen; $count++)
    {
      if($groupList[$count] == $currentGroup)
      {                
        return $groupList[$count];
      }           
    }
  }
  
  function getGroupItems($groupName)
  {
    global $gGalleryPath;
    
    $galleryDir = $gGalleryPath.'/'.$groupName;
    $fileArray = scandir($galleryDir,0);
    $fileArrayLen = sizeof($fileArray);
    
    $count = 0;
    $ret = ""; 
    for(;$count < $fileArrayLen; $count++)
    {
      if(validateFile($fileArray[$count]) == TRUE)
      {
        $ret[] = $fileArray[$count];
      }
    }
    
    return $ret;
  }
  
  function getMetaInfo($groupName, $language, $groupInfo)
  {
    global $gGalleryPath;
    global $gDefaultLanguage;
    
    $groupTitle = "";
    
    if($language == NULL)
    {
      $language = $gDefaultLanguage;
    }
    $infoFile = $gGalleryPath.'/'.$groupName.'/'."info.xml";
    if(file_exists($infoFile))
    {
      $xmlContent = file_get_contents($infoFile);
      $xml = new SimpleXMLElement($xmlContent);
      if($language == "english"){
        if($xml->title[0]->english == NULL || $xml->title[0]->english == "")
        {
          $groupInfo->groupTitle = (string)$xml->title[0]->chinese[0]; //convert it to string instead of a object.
        }
        else
        {
          $groupInfo->groupTitle = (string)$xml->title[0]->english[0]; //convert it to string instead of a object.
        }
      }
      else //chinese
      {
        if($xml->title[0]->chinese == NULL || $xml->title[0]->chinese == "")
        {
          $groupInfo->groupTitle = (string)$xml->title[0]->english[0]; //convert it to string instead of a object.
        }
        else
        {
          $groupInfo->groupTitle = (string)$xml->title[0]->chinese[0]; //convert it to string instead of a object.
        }        
      }
      
      $len = count($xml->item);
      $index = 0;
      for(; $index < $len; $index++)
      {
        $groupInfo->fileDescriptions[] = (string)$xml->item[$index];
      }
      
    }
    
    return $groupTitle;
  }
  
  function validateFile($fileName)
  {
    $len = strlen($fileName);
    if($len < 4)
    {
      return FALSE;
    }
    if($fileName[0] == '.')
    {
      return FALSE;
    }
    $path_parts = pathinfo($fileName);
    $extension = strtolower($path_parts['extension']);
   
    if($extension != 'jpg' && $extension != 'jpeg' && $extension != 'png' && $extension != 'gif')
    {
      return FALSE;
    }

    return TRUE;
  }

  
?>